package nccloud.arap.bill.caldisc.action;

import java.util.HashMap;
import java.util.Map;

import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.MapProcessor;
import nc.md.model.MetaDataException;
import nc.vo.platform.workbench.AppParamVO;
import nc.vo.platform.workbench.AppRegisterVO;
import nc.vo.platform.workbench.model.AppRegisterAggVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.AppContext;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.RequestSysJsonVO;
import nccloud.pubitf.platform.workbench.IAppRegister;

public class cardCreateCalDiscAction implements ICommonAction {

	@Override
	public Object doAction(IRequest paramIRequest) {
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		String pk_group = AppContext.getInstance().getPkGroup();
		RequestSysJsonVO sysvo = paramIRequest.getRequestJsonVO().getSysParamJson();
		String appcode = sysvo.getAppcode();
		
		putGroup(result, pk_group);
		putTranstype(result, appcode);
		
		
		//获取当前时间
		result.put("dbilldate",  AppContext.getInstance().getBusiDate());
	
		return result;
	}



	/**
	 * 交易类型初始话
	 * 
	 * @param result
	 * @param appcode
	 */
	private void putTranstype(Map<String, Object> result, String appcode) {
		IAppRegister appServer = ServiceLocator.find(IAppRegister.class);
		
		AppRegisterVO appVO = appServer.queryAppVOByCode(appcode, true);
		AppRegisterAggVO billvo = null;
		try {
			billvo = appServer.queryAppRegisterAggVO(appVO.getPrimaryKey(), true);
		} catch (MetaDataException e) {
			e.printStackTrace();
		}
		
		AppParamVO[] paramvos = billvo.getAppParamVOs();
		
		for(AppParamVO param : paramvos) {
			
			if("pk_transtype".equals(param.getParamname())) {
				
				Map<String, Object> inMap = new HashMap<String, Object>();
				
				inMap.put("value", param.getParamvalue());
				inMap.put("display", appVO.getName());
				
				result.put(param.getParamname(), inMap);
			}else {
				result.put(param.getParamname(), param.getParamvalue());
			}
		}
	}

	/**
	 * 集团初始话
	 * 
	 * @param result
	 * @param pk_group
	 */
	private void putGroup(Map<String, Object> result, String pk_group) {
		
		String sql = "select pk_group as value, name as display from org_group where pk_group = '"+pk_group+"'";
		
		Map<String, Object> map = null;
		
		try {
			map = (Map<String, Object>) ServiceLocator.find(IUAPQueryBS.class).executeQuery(sql, new MapProcessor());
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		
		result.put("pk_group", map);
	}

}
